package com.jemmyee.pizza.web.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.CostOther;
import com.jemmyee.pizza.entity.Person;
import com.jemmyee.pizza.entity.Prepaid;
import com.jemmyee.pizza.service.ICostOtherService;
import com.jemmyee.pizza.service.IPrepaidService;
import com.jemmyee.pizza.web.Constants;
import com.jemmyee.pizza.web.bean.PrepaidBean;

@Controller
@RequestMapping("/admin/prepaid")
public class PrepaidController {
	private static Log log = LogFactory.getLog(PrepaidController.class);
	@Autowired
	private IPrepaidService<Prepaid> prepaidService;
	@Autowired
	private ICostOtherService<CostOther> costOtherService ;
	
	@RequestMapping("/list/{from}/{startIndex}")
	public ModelAndView list(@PathVariable Integer startIndex,@PathVariable String from,@ModelAttribute PrepaidBean bean,HttpServletRequest request) {
		DetachedCriteria dc = DetachedCriteria.forClass(Prepaid.class);
		PrepaidBean prepaidBean = new PrepaidBean();
		if (from.equals("form")) {
			prepaidBean = bean;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, prepaidBean);
		}
		if (from.equals("page")) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				prepaidBean = (PrepaidBean) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
		}
		if (from.equals("menu")) {
			System.out.println("menu is call");
		}

		if(prepaidBean.getName() != null && !prepaidBean.getName().equals("")){
			dc.add(Restrictions.like("prepaidName", prepaidBean.getName(), MatchMode.ANYWHERE));
		}
		
		if(prepaidBean.getNumber() != null && !prepaidBean.getNumber().equals("")){
			dc.add(Restrictions.like("prepaidId", prepaidBean.getNumber(), MatchMode.ANYWHERE));
		}
		
		if(prepaidBean.getAddr() != null && !prepaidBean.getAddr().equals("")){
			dc.add(Restrictions.like("prepaidAddress", prepaidBean.getAddr(), MatchMode.ANYWHERE));
		}
		// 发布时间
		if (prepaidBean.getStartTime() != null) {
			dc.add(Restrictions.ge("createTime", prepaidBean.getStartTime()));
		}
		if (prepaidBean.getEndTime() != null) {
			dc.add(Restrictions.le("createTime", prepaidBean.getEndTime()));
		}

//		dc.addOrder(Order.desc("createTime"));
		ModelAndView mv = new ModelAndView();
		Page page = prepaidService.findPageByCriteria(dc,Constants.DEFAULT_PAGESIZE, startIndex,"createTime");
		mv.addObject("page", page);
		mv.addObject("prepaidBean", prepaidBean);
		mv.setViewName("/admin/prepaid/list");
		return mv;
	}
	
	
	@RequestMapping("/view/{id}")
	public ModelAndView view(@PathVariable Integer id) {
		ModelAndView mv = new ModelAndView();
		Prepaid prepaid = prepaidService.findById(id);
		mv.setViewName("/admin/prepaid/view");
		mv.addObject("prepaid",prepaid);
		return mv;
	}
	
	@RequestMapping("/to_edit/{id}")
	public ModelAndView toEdit(@PathVariable Integer id){
	   ModelAndView mv=new ModelAndView();
	   Prepaid prepaid = prepaidService.findById(id);
	   mv.setViewName("/admin/prepaid/edit");
	   mv.addObject("prepaid",prepaid);
	   return mv;
	}
	
	@RequestMapping("/edit")
	@ResponseBody
	public Map<String, Object> edit(@RequestBody Prepaid prepaid,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		Prepaid p = prepaidService.findById(prepaid.getPrepaidId());
		if(p == null){
			model.put("msg","error");
		}else{
			if(!p.getPrepaidName().equals(prepaid.getPrepaidName())){
				CostOther co = new CostOther();
				co.setCustomCode(String.valueOf(p.getPrepaidId()));
				co.setCustomName(p.getPrepaidName());
				co.setAddress(p.getPrepaidAddress());
				co.setTel(p.getPrepaidTel());
				co.setPrice(Float.valueOf(30));
				co.setCostName("用户过户");
				co.setCoster((String)request.getSession().getAttribute(Constants.ONLINE_USERNAME));
				co.setCostDate(new Date());
				co.setRemark(p.getPrepaidName()+" 过户给 "+prepaid.getPrepaidName());
				costOtherService.save(co);
			}
			p.setPrepaidName(prepaid.getPrepaidName());
			p.setMeterName(prepaid.getMeterName());
			p.setPrepaidType(prepaid.getPrepaidType());
			p.setPrepaidPrice(prepaid.getPrepaidPrice());
			p.setPrepaidAddress(prepaid.getPrepaidAddress());
			p.setPrepaidTel(prepaid.getPrepaidTel());
			p.setRemark(prepaid.getRemark());
			prepaidService.update(p);
			model.put("msg","succ");
		}
		return model;
	}
	/*
	@RequestMapping("/to_delete/{id}")
	public ModelAndView toDelete(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		Prepaid prepaid = prepaidService.findById(id);
		mv.setViewName("/admin/prepaid/list");
		mv.addObject("prepaid",prepaid);
		prepaidService.delete(id);
		System.out.print("aaaa"+prepaid);
		
		return mv;
	}*/
	
	@RequestMapping("/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable Integer id,HttpServletRequest request)
	{   
		Map<String, Object> model = new HashMap<String, Object>();
		Prepaid p = prepaidService.findById(id);
		if(p == null){
			model.put("msg","error");
		}else{
			//System.out.println(p+"在这里 执行了 删除");
			prepaidService.delete(p);
			model.put("msg","succ");
		}
		return model;
	}
}
